package cp3120.dao.mysql;

import cp3120.dao.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author Adam Scarr <scarr.adam@gmail.com>
 */
public class MySQLDAOFactory extends DAOFactory {
    private String databaseURL = "jdbc:mysql://dunk.it.jcu.edu.au/3120S109g08";
    private String userName = "3120S109g08";
    private String password = "e7d74ec6f6";
    private Connection db;
    private MySQLFlightDAO flightDAO;
    private MySQLUserDAO userDAO;
    private MySQLPurchaseDAO purchaseDAO;
    private MySQLVariableDAO variableDAO;

    /**
     * Default constructor connects to a database using default varibles
     */
    public MySQLDAOFactory() {
        connect();
    }

    /**
     * Creates a new MySQLDAOFactory and connects to a mysql database.
     * @param databaseURL   The URL of the database to connect too
     * @param userName      the login name of the database
     * @param password      the password for the database
     */
    public MySQLDAOFactory(String databaseURL, String userName, String password) {
        this.databaseURL = databaseURL;
        this.userName = userName;
        this.password = password;
        connect();
    }

    /**
     * Opens the connection to the MySQL database
     * All public methods bar conenct() and disconnect() must first call connect()
     */
    public void connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            db = DriverManager.getConnection(databaseURL, userName, password);
        } catch (SQLException e) {
            System.out.println("Database connection error: " + e.getMessage());
            e.printStackTrace();
            System.exit(0);
        } catch (ClassNotFoundException e) {
            System.out.println("Error loading JDBC Driver: " + e.getMessage());
            System.exit(0);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        flightDAO = new MySQLFlightDAO(db);
        userDAO = new MySQLUserDAO(db);
        purchaseDAO = new MySQLPurchaseDAO(db);
        variableDAO = new MySQLVariableDAO(db);
    }

    /**
     * Closes the connection to the MySQL database
     * All public methods bar conenct() and disconnect() must call disconnect() before returning
     */
    public void disconnect() {
        try {
            db.close();
            System.out.println("Database connection terminated");
        } catch (Exception e) { /* ignore close errors */ }

    }

    /**
     * @return The DAO for flights.
     */
    public FlightDAO getFlightDAOImplementation() {
        return flightDAO;
    }

    /**
     * @return The DAO for users.
     */
    public UserDAO getUserDAOImplementation() {
        return userDAO;
    }

    /**
     * @return The DAO for purchases.
     */
    public PurchaseDAO getPurchaseDAOImplementation() {
        return purchaseDAO;
    }

    /**
     * @return The DAO for variables
     */
    protected VariableDAO getVariableDAOImplementation() {
        return variableDAO;
    }
}
